package com.ruoyi.mine.controller;

import com.ruoyi.mine.service.MineFileService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.mine.dto.MineFileInfoUpdateDTO;
import com.ruoyi.mine.dto.MineFileUploadDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.mine.pojo.MineFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@Api(tags = "矿山文件管理相关接口", value = "MySQL表主导执行(接口除查询外均已完成测试)")
@RequestMapping("/mine/files")
@RestController
public class MineFileController {

    @Autowired
    private MineFileService mineFileService;

    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "上传矿山文件数据接口")
    @PostMapping("/upload")
    public R<String> upload(@ModelAttribute @Validated(MineFileUploadDTO.Upload.class) MineFileUploadDTO mineFileUploadDTO) throws IOException {

        mineFileService.upload(mineFileUploadDTO);
        return R.ok("矿山文件及信息上传成功");
    }

    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "删除矿山文件数据接口", notes = "文件删除代码已添加并成功完成测试")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") @NotNull(message = "矿山文件ID不能为空") Integer id) {

        mineFileService.delete(id);
        return R.ok("矿山文件及信息删除成功");
    }

    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "更新矿山文件数据接口", notes = "(当前不进行设置)后续可能对文件移动的操作进行处理")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(MineFileInfoUpdateDTO.Update.class) MineFileInfoUpdateDTO mineFileUploadDTO) {

        mineFileService.update(mineFileUploadDTO);
        return R.ok("矿山文件信息更新成功");
    }

    @DataSource(value = DataSourceType.MASTER)
    @ApiOperation(value = "查询矿山文件数据接口", notes = "查询需要矿山表(mine),需要对跨数据库表进行操作")
    @GetMapping("/search")
    public R<List<MineFile>> search(@RequestParam(name = "mineId", required = false) String mineId,
                                    @RequestParam(name = "fileType", required = false) String fileType,
                                    @RequestParam(name = "fileName", required = false) String fileName) {

        List<MineFile> mineFileList = mineFileService.search(mineId, fileType, fileName);
        mineFileList.forEach(mineFile -> mineFile.setMine(mineFileService.getMine(mineFile.getMineId())));
        return R.ok(mineFileList, "矿山文件查询成功");
    }
}
